import { CylinderCollider, find, Label, Sprite, SpriteFrame, Vec2, view } from 'cc';
import { tween } from 'cc';
import { UITransform } from 'cc';
import { Tween } from 'cc';
import { ColliderComponent } from 'cc';
import { ITriggerEvent } from 'cc';
import { Texture2D } from 'cc';
import { Vec3 } from 'cc';
import { MeshRenderer } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { App } from '../../../App';
import UtilTool from '../../../tools/UtilTool';
import { Ball } from '../Ball';
import { GamePlayState } from '../GameBaseLogic';
import { GameBloodLogic } from './GameBloodLogic';
import { GameLayer5 } from './GameLayer5';
import { GameModel5 } from './GameModel5';
const { ccclass, property } = _decorator;

// 得分区域界面
@ccclass('GameBloodArea')
export class GameBloodArea extends Component {
    @property(Node)
    node3D : Node = null

    @property(Node)
    areaWhite : Node = null

    @property(Node)
    whSpr : Node = null

    @property(Node)
    areaYellow : Node = null

    @property({ type: MeshRenderer })
    score3D: MeshRenderer = null

    // 本地坐标
    pos3D = new Vec3(0, 0, 0)

    // 激活状态 [0]还没激活 [1]已激活
    status = 0 

    // 区域所属分数
    score = 0  
    
    // 区域等级
    level = 1 // 1~9范围 1最大区域，分数最小

    start() {
        this.areaWhite.getComponent(CylinderCollider).on('onTriggerStay', this.onTriggerArea, this);
        
    }

    /** 碰撞检测 */
    onTriggerArea(event: ITriggerEvent) {

        if(event.otherCollider.node.name == "ball" || event.otherCollider.node.name == "whiteball") {
            if (this.status == 0) {
                this.updateView(1)

                let GameLogic = App.Model.gameLogic as GameBloodLogic
                if(GameLogic.areaCollideList.indexOf(this.level)==-1) {
                    UtilTool.printLog("cjw test areaCollideList", this.level)
                    GameLogic.areaCollideList.push(this.level)
                }
            }
        }
        // let ball = event.otherCollider.node;
        // let ballCtrl = ball.getComponent(Ball)
    }

    /**
     * 初始化区域属性
     */
    init(key, value) {
        this.score = value.score
        this.level = parseInt(key)
        this.pos3D.x = value.posX
        this.pos3D.y = 0
        this.pos3D.z = value.posZ

        if (this.level<1 || this.level>9) {
            this.level = 1
        }
        if (this.score<1 || this.score>9) {
            this.score = 1
        }

        this.updateView(0)
    }

    /**刷新界面 */
    updateView(status) {
        this.status = status

        // 坐标
        let gameView = App.Model.gameView as GameModel5
        this.node.setWorldPosition(this.pos3D)

        if (this.status == 0) { // 未激活
            this.score3D.setMaterial(gameView.scoreMaterial[this.score-1], 0);

            this.setSize()

            this.areaWhite.active = true
            this.whSpr.active = true
            this.areaYellow.active = false
            this.score3D.node.active = false
        }
        else if (this.status == 1) { // 已激活
            this.whSpr.active = false
            this.score3D.node.active = true
            
            // 播放激活状态动画
            this.areaYellow.active = true
            this.areaYellow.setScale(0.01, 1, 0.01)
            tween(this.areaYellow)
                .to(0.6, {scale: new Vec3(4.5, 1, 4.5)})
                .call(() => {
                    this.areaWhite.active = false
                })
                .start()
        }
    }

    /**设置区域大小 */
    setSize() {
        let rate = 0.35 + (9-this.level)*0.081 // 这个计算方式相当于每个之间减少20个像素
        this.node3D.setScale(rate,1,rate)
    }

    onDestroy() {
        
    }

}

